Logical Functions and IF Statements in Tableau – Complete Detailed Notes
Topic: Logical Functions and IF Statements in Tableau
Course: JustAcademy Tableau Training
Course Demo: Register for a Tableau Course Demo
Learning Objectives: Understand logical functions, write IF statements, combine conditions using AND, OR, and NOT, handle NULL values, categorize data, and use logical calculations in Tableau worksheets and dashboards.
1. Introduction to Logical Functions in Tableau
Logical functions and expressions in Tableau help evaluate conditions and determine which result should be returned. They are widely used to classify data, create business rules, check target achievement, identify profitable products, and control the information displayed in reports.
For example, a company may want to classify products as Profitable, Break Even, or Loss based on their profit values. A logical calculation can automatically assign the correct category to each record.
Example
IF [Profit] > 0 THEN "Profitable"
ELSEIF [Profit] = 0 THEN "Break Even"
ELSE "Loss"
END
This calculation checks profit and returns a category based on the first condition that evaluates to TRUE.
2. What Is Logical Evaluation?
Logical evaluation determines whether a condition is TRUE, FALSE, or sometimes NULL when the result is unknown. Tableau uses logical expressions to test values and make decisions within calculations.
| Logical Result |
Meaning |
Example |
| TRUE |
The condition is satisfied. |
[Sales] > 5000 when sales are 8000. |
| FALSE |
The condition is not satisfied. |
[Sales] > 5000 when sales are 3000. |
| NULL |
The result is unknown, often because an input is missing. |
[Sales] > 5000 when Sales is NULL. |
Understanding the difference between FALSE and NULL is important. FALSE means a condition has been evaluated and is not satisfied. NULL means Tableau cannot determine the result from the available values.
3. Introduction to IF Statements
An IF statement evaluates a condition and returns a specified result when the condition is TRUE. It can also include additional conditions and a default result for cases where earlier conditions are not satisfied.
Basic Syntax
IF condition THEN result
END
Example
IF [Sales] > 10000 THEN "High Sales"
END
If sales exceed 10,000, the result is High Sales. If the condition is not TRUE, the calculation returns NULL because no ELSE result is specified.
Example Data
| Sales |
Result |
| 15000 |
High Sales |
| 12000 |
High Sales |
| 8000 |
NULL |
| 5000 |
NULL |
4. Components of an IF Statement
| Keyword |
Purpose |
Example |
IF |
Starts the conditional expression. |
IF [Sales] > 5000 |
THEN |
Specifies the result when the condition is TRUE. |
THEN "High" |
ELSEIF |
Checks another condition when the earlier condition is not TRUE. |
ELSEIF [Sales] > 2000 |
ELSE |
Provides a default result when no earlier condition is TRUE. |
ELSE "Low" |
END |
Closes the IF expression. |
END |
5. Using IF, THEN, ELSE, and END
The IF statement can contain a single condition and a default result. The ELSE clause is optional, but including it is often useful when you want every record to receive a meaningful category.
Syntax
IF condition THEN result
ELSE default_result
END
Example: Check Sales Performance
IF [Sales] >= 10000 THEN "Target Level"
ELSE "Below Target"
END
Records with sales of at least 10,000 receive Target Level. All other records receive Below Target.
| Sales |
Category |
| 15000 |
Target Level |
| 10000 |
Target Level |
| 7500 |
Below Target |
| 4000 |
Below Target |
6. Using ELSEIF for Multiple Conditions
The ELSEIF keyword allows you to evaluate additional conditions after the initial IF condition is not TRUE. You can use multiple ELSEIF clauses to create several categories.
Syntax
IF condition1 THEN result1
ELSEIF condition2 THEN result2
ELSEIF condition3 THEN result3
ELSE default_result
END
Example: Categorize Sales
IF [Sales] >= 15000 THEN "Excellent"
ELSEIF [Sales] >= 10000 THEN "Good"
ELSEIF [Sales] >= 5000 THEN "Average"
ELSE "Low"
END
| Sales |
Category |
| 20000 |
Excellent |
| 12000 |
Good |
| 7000 |
Average |
| 3000 |
Low |
Important: Tableau returns the result for the first condition that evaluates to TRUE. Order the conditions carefully, especially when using overlapping thresholds.
7. Logical AND Operator
The AND operator combines two or more conditions. The combined condition is TRUE only when all conditions are TRUE.
Syntax
condition1 AND condition2
Example: Identify High Sales With Positive Profit
IF [Sales] > 10000 AND [Profit] > 0 THEN
"High Sales and Profitable"
ELSE
"Other"
END
Both conditions must be TRUE for a record to receive High Sales and Profitable.
| Sales |
Profit |
Result |
| 15000 |
3000 |
High Sales and Profitable |
| 15000 |
-1000 |
Other |
| 8000 |
2000 |
Other |
| 5000 |
-500 |
Other |
Common Uses of AND
- Identify products with high sales and positive profit.
- Find employees whose performance and attendance meet requirements.
- Identify orders within a specified price range.
- Check whether actual sales meet a target and profit remains positive.
8. Logical OR Operator
The OR operator combines conditions and returns TRUE when at least one condition is TRUE. Both conditions do not need to be TRUE.
Syntax
condition1 OR condition2
Example: Identify High Sales or High Profit
IF [Sales] > 15000 OR [Profit] > 5000 THEN
"High Performance"
ELSE
"Standard Performance"
END
A record is classified as High Performance if sales exceed 15,000 or profit exceeds 5,000.
| Sales |
Profit |
Result |
| 20000 |
3000 |
High Performance |
| 12000 |
6000 |
High Performance |
| 10000 |
2000 |
Standard Performance |
Common Uses of OR
- Identify customers from either of two regions.
- Find products that have high sales or high profit.
- Identify orders that qualify through any one of several business rules.
- Combine alternative conditions into one calculated field.
9. Logical NOT Operator
The NOT operator reverses a logical result. TRUE becomes FALSE, and FALSE becomes TRUE. If the original expression is NULL, NOT generally remains NULL.
Syntax
NOT condition
Example: Identify Records Outside the West Region
IF NOT ([Region] = "West") THEN "Other Region"
ELSE "West Region"
END
This calculation checks whether the region is not West. A NULL region does not automatically count as a known non-West region; handle missing regions explicitly if required.
Another Example
NOT ([Profit] > 0)
This expression is TRUE when the profit comparison is FALSE, such as when profit is zero or negative. It may return NULL if profit itself is NULL.
10. Combining AND, OR, and NOT
You can combine logical operators to create more advanced conditions. Use parentheses to make the intended evaluation order clear.
Example 1: High Sales in Selected Regions
IF [Sales] > 10000
AND ([Region] = "East" OR [Region] = "West")
THEN "Priority Sales"
ELSE "Other Sales"
END
This identifies records with sales above 10,000 that belong to either the East or West region.
Example 2: Positive Profit Outside One Region
IF [Profit] > 0 AND NOT ([Region] = "South")
THEN "Eligible"
ELSE "Not Eligible"
END
This checks whether profit is positive and the region is not South. Consider adding a separate NULL check if missing region values must be distinguished.
Example 3: Target or Profit Requirement
IF [Sales] >= [Sales Target]
OR [Profit] >= 5000
THEN "Meets Business Rule"
ELSE "Does Not Meet Rule"
END
The record qualifies if it meets the sales target or reaches the specified profit threshold.
11. The IIF Function
IIF() is a logical function that checks a condition and returns one result when the condition is TRUE and another result when it is FALSE. It can also specify a separate result for an unknown or NULL condition.
Syntax
IIF(test, then_value, else_value, unknown_value)
The fourth argument is optional. If it is omitted and the condition is NULL, the result is NULL.
Example
IIF([Profit] > 0, "Profitable", "Not Profitable")
This returns Profitable when profit is positive and Not Profitable when the condition is FALSE. If the comparison is NULL, the result is NULL unless an unknown result is specified.
Example With NULL Handling
IIF([Profit] > 0, "Profitable", "Not Profitable", "Unknown")
This returns Unknown when the condition cannot be determined because it is NULL.
IF Versus IIF
| Feature |
IF Statement |
IIF Function |
| Structure |
Uses IF, THEN, ELSEIF, ELSE, and END. |
Uses function arguments separated by commas. |
| Multiple conditions |
Supports multiple ELSEIF clauses. |
Nested IIF functions are often needed for multiple conditions. |
| NULL condition |
Handled through the IF expression's condition logic and ELSE result. |
Can use a separate optional unknown argument. |
| Readability |
Often clearer for several conditions. |
Convenient for a simple two-way decision. |
12. The CASE Statement
The CASE statement compares one expression against several possible values and returns the corresponding result for the first match. It is useful when a single field must be mapped to different categories.
Syntax
CASE expression
WHEN value1 THEN result1
WHEN value2 THEN result2
WHEN value3 THEN result3
ELSE default_result
END
Example: Categorize Regions
CASE [Region]
WHEN "East" THEN "Eastern Region"
WHEN "West" THEN "Western Region"
WHEN "South" THEN "Southern Region"
WHEN "Central" THEN "Central Region"
ELSE "Other or Unknown"
END
This formula checks the value of Region and returns the matching label. If no value matches, it returns Other or Unknown.
IF Versus CASE
| IF |
CASE |
| Can test different conditions, including comparisons and multiple fields. |
Compares one expression with a list of specified values. |
| Useful for ranges and complex business rules. |
Useful for mapping known values to labels. |
| Supports ELSEIF clauses. |
Uses WHEN clauses. |
Example: IF [Sales] > 10000 THEN "High" ELSE "Low" END |
Example: CASE [Region] WHEN "West" THEN "W" ELSE "Other" END |
13. Handling NULL Values With Logical Functions
NULL represents missing or unknown information. Tableau provides several functions to test for or replace NULL values. These functions are useful when logical calculations depend on incomplete source data.
13.1 ISNULL()
ISNULL() checks whether an expression is NULL. It returns TRUE if the expression is NULL and FALSE otherwise.
ISNULL([Sales])
13.2 IFNULL()
IFNULL() returns the first expression when it is not NULL; otherwise, it returns the replacement expression.
IFNULL([Sales], 0)
This returns Sales when available and zero when Sales is NULL. Use zero only when it is appropriate to treat missing sales as zero for the intended analysis.
13.3 ZN()
ZN() returns the original numeric expression if it is not NULL and returns zero if it is NULL.
ZN([Profit])
13.4 IFNULL() Versus ISNULL() Versus ZN()
| Function |
Purpose |
Example |
ISNULL() |
Tests whether a value is NULL. |
ISNULL([Sales]) |
IFNULL() |
Replaces NULL with a specified alternative. |
IFNULL([Sales], 0) |
ZN() |
Replaces a NULL numeric value with zero. |
ZN([Profit]) |
Example: Categorize Missing Sales
IF ISNULL([Sales]) THEN "Missing Sales"
ELSEIF [Sales] >= 10000 THEN "High Sales"
ELSE "Low Sales"
END
This calculation identifies missing sales values separately from known low sales values.
14. Creating a Profit Status Calculation
A profit status field can categorize each record based on its profit value.
Formula
IF [Profit] > 0 THEN "Profit"
ELSEIF [Profit] = 0 THEN "Break Even"
ELSEIF [Profit] < 0 THEN "Loss"
ELSE "Unknown"
END
The final ELSE clause helps account for values that do not match the three comparisons, including NULL profit values.
| Profit |
Profit Status |
| 5000 |
Profit |
| 0 |
Break Even |
| -2000 |
Loss |
| NULL |
Unknown |
15. Creating a Sales Performance Classification
A sales performance classification groups records into categories according to their sales value. It is useful for business reports, conditional colors, and dashboards.
Formula
IF [Sales] >= 20000 THEN "Excellent"
ELSEIF [Sales] >= 10000 THEN "Good"
ELSEIF [Sales] >= 5000 THEN "Average"
ELSEIF NOT ISNULL([Sales]) THEN "Low"
ELSE "Unknown"
END
This calculation classifies known sales values and assigns Unknown to missing values. The order of conditions ensures that higher thresholds are evaluated first.
16. Creating a Customer Priority Calculation
Businesses can combine sales and profit conditions to identify customers or orders that need attention.
Formula
IF [Sales] >= 15000 AND [Profit] > 0 THEN "High Priority"
ELSEIF [Sales] >= 10000 OR [Profit] >= 3000 THEN "Medium Priority"
ELSE "Standard Priority"
END
This calculation assigns High Priority when both high sales and positive profit conditions are satisfied. Otherwise, it checks the medium-priority condition. The remaining records receive Standard Priority.
When missing values are possible, consider adding explicit NULL checks so that unknown information is not accidentally treated as an ordinary business category.
17. Steps to Create a Logical Calculated Field in Tableau
- Open Tableau and connect to your dataset.
- Open a worksheet.
- Select Analysis > Create Calculated Field, or use the corresponding option in the Data pane.
- Enter a descriptive name, such as
Profit Status.
- Write the logical formula in the calculation editor.
- Check the formula for syntax errors and invalid field references.
- Click OK to save the calculated field.
- Locate the new field in the Data pane.
- Drag the field to Rows, Columns, Color, Label, Tooltip, or Filters, as appropriate.
- Compare the displayed results against sample records to confirm the logic is correct.
18. Using Logical Calculations in Tableau Visualizations
Logical calculations can improve visualizations by creating categories and conditions that are easier to interpret than raw numbers.
- Color: Highlight profitable and loss-making products with separate colors.
- Rows: Group records by calculated categories.
- Columns: Display calculated measures or categories in the view.
- Filters: Filter records based on a logical condition.
- Labels: Display a status such as Target Achieved or Below Target.
- Tooltips: Explain a product's performance when a user hovers over a mark.
Example: Sales by Region
- Create a calculated field called
Sales Category.
- Use an IF statement to classify sales as High, Medium, or Low.
- Drag Region to Rows.
- Drag Sales to Columns.
- Drag Sales Category to Color.
- Review the chart to compare categories across regions.
19. Common Errors and Troubleshooting
| Problem |
Possible Cause |
Solution |
| Missing END error |
The IF or CASE expression is not closed. |
Add the required END keyword. |
| Incorrect category |
Conditions are ordered incorrectly. |
Review the order and thresholds of IF and ELSEIF clauses. |
| Unexpected NULL result |
No ELSE clause exists or an input is NULL. |
Add suitable default logic and check source values. |
| Invalid comparison |
Incompatible data types are being compared. |
Confirm field types and use an appropriate conversion if needed. |
| Incorrect AND/OR result |
The conditions do not match the intended business rule. |
Review each condition and add parentheses for clarity. |
| Aggregate/non-aggregate error |
The formula mixes aggregate and row-level expressions improperly. |
Make the calculation consistent with the intended level of aggregation. |
| Missing data incorrectly classified |
NULL values are not explicitly handled. |
Use ISNULL() or another suitable NULL-handling expression. |
| Incorrect result from IIF |
The condition is NULL rather than FALSE. |
Provide the optional unknown argument if a specific NULL result is required. |
20. Best Practices for Logical Functions and IF Statements
- Use meaningful names for calculated fields, such as
Sales Performance or Profit Status.
- Write conditions in a clear and readable order.
- Use ELSE to define a default category when appropriate.
- Use ELSEIF for multiple conditions rather than deeply nested IF statements when possible.
- Use CASE when matching one field against a list of known values.
- Use AND when every condition must be satisfied.
- Use OR when any one condition can qualify a record.
- Use NOT carefully because it reverses the logical result but does not turn NULL into a known value.
- Handle missing values explicitly when they matter to the analysis.
- Test boundary values, including values exactly equal to a threshold.
- Use parentheses when combining AND and OR to make the intended logic clear.
- Validate results in a worksheet before publishing a dashboard.
21. Practical Project: Sales Performance Dashboard
Scenario: A retail company wants to classify sales performance, evaluate profitability, and identify whether sales targets are achieved.
Sample Data
| Product |
Sales |
Profit |
Target |
| Laptop |
25000 |
5000 |
20000 |
| Mobile |
15000 |
2000 |
18000 |
| Tablet |
8000 |
-1000 |
10000 |
| Monitor |
5000 |
0 |
5000 |
Task 1: Create a Sales Category
IF [Sales] >= 20000 THEN "High"
ELSEIF [Sales] >= 10000 THEN "Medium"
ELSE "Low"
END
Task 2: Create a Profit Status
IF [Profit] > 0 THEN "Profitable"
ELSEIF [Profit] = 0 THEN "Break Even"
ELSE "Loss"
END
Task 3: Check Target Achievement
IF [Sales] >= [Target] THEN "Target Achieved"
ELSE "Below Target"
END
Expected Results
| Product |
Sales Category |
Profit Status |
Target Status |
| Laptop |
High |
Profitable |
Target Achieved |
| Mobile |
Medium |
Profitable |
Below Target |
| Tablet |
Low |
Loss |
Below Target |
| Monitor |
Low |
Break Even |
Target Achieved |
Task 4: Build the Dashboard View
- Place Product on Rows.
- Place Sales on Columns.
- Use Sales Category on Color.
- Add Profit Status and Target Status to the view or Tooltip.
- Review the results and compare products by sales, profitability, and target achievement.
Expected Learning Outcome: You will learn how to use logical conditions to classify records and make business performance easier to understand.
22. Interview Questions and Answers
Q1. What are logical functions in Tableau?
Answer: Logical functions and expressions evaluate conditions and return results based on TRUE, FALSE, or unknown conditions.
Q2. What is an IF statement?
Answer: An IF statement evaluates a condition and returns a specified result when the condition is TRUE. ELSEIF and ELSE can be used for additional conditions and default results.
Q3. What is the purpose of ELSEIF?
Answer: ELSEIF checks another condition when the earlier IF or ELSEIF condition is not TRUE.
Q4. What is the purpose of ELSE?
Answer: ELSE returns a default result when none of the preceding conditions is TRUE.
Q5. What is the difference between AND and OR?
Answer: AND requires all combined conditions to be TRUE, whereas OR requires at least one condition to be TRUE.
Q6. What does NOT do?
Answer: NOT reverses a logical result, turning TRUE into FALSE and FALSE into TRUE. A NULL result generally remains unknown.
Q7. What is the difference between IF and IIF?
Answer: IF supports multiple ELSEIF conditions in a readable block structure. IIF is a function that evaluates one test and returns a true result, a false result, and optionally a separate result for an unknown condition.
Q8. What is CASE used for?
Answer: CASE compares a single expression against a list of possible values and returns the result associated with a matching value.
Q9. How do you handle NULL values in Tableau?
Answer: Use ISNULL() to test for NULL, IFNULL() to provide a replacement value, or ZN() to replace a NULL numeric expression with zero.
Q10. Why might an IF statement return NULL?
Answer: It may return NULL when no condition is TRUE and no ELSE clause is provided, or when the result depends on NULL values.
Q11. Can IF statements be used to categorize sales?
Answer: Yes. IF and ELSEIF can classify sales into categories such as High, Medium, and Low based on defined thresholds.
Q12. Why is condition order important?
Answer: Tableau returns the result for the first condition that evaluates to TRUE. Conditions should be ordered so that overlapping thresholds produce the intended category.
23. Practice Exercises
- Create an IF statement that classifies sales above 15,000 as High and other sales as Low.
- Create a three-level sales category using IF and ELSEIF.
- Use AND to identify products with sales above 10,000 and positive profit.
- Use OR to identify products with sales above 20,000 or profit above 5,000.
- Use NOT to identify records outside a specified region.
- Use CASE to map region names to custom labels.
- Use IIF() to check whether a product is profitable.
- Use ISNULL() to identify missing sales values.
- Use IFNULL() to replace missing numeric values where appropriate.
- Create a target achievement field using actual sales and target sales.
- Test every formula with values above, below, and exactly at its threshold.
- Build a worksheet that displays the categories using colors and labels.
24. Quick Revision Checklist
- Understand TRUE, FALSE, and NULL.
- Know the syntax of IF, THEN, ELSEIF, ELSE, and END.
- Combine conditions with AND, OR, and NOT.
- Understand when to use IF, IIF, and CASE.
- Use ISNULL(), IFNULL(), and ZN() appropriately.
- Order multiple conditions carefully.
- Handle missing values and default results intentionally.
- Use logical calculations in filters, colors, labels, and tooltips.
- Test boundary values and verify the output.
- Apply logical calculations to a practical business dashboard.
25. Conclusion
Logical functions and IF statements are essential Tableau skills for turning raw data into meaningful business categories and decisions. IF and ELSEIF help evaluate multiple conditions, AND and OR combine logical tests, NOT reverses a condition, CASE maps known values to results, and functions such as ISNULL() and IFNULL() help manage missing data.
With consistent practice, you can create calculated fields for sales classification, profitability, target achievement, customer priority, and other business requirements. Always test conditions carefully and handle NULL values explicitly when they affect your analysis.
26. Tableau Training and Course Demo
Explore the following resources to continue learning Tableau:
27. Official Tableau Learning Resources